//
//  DENFindDetailViewController.m
//  Icar
//
//  Created by apple on 15/10/20.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "DENFindDetailViewController.h"
#import "BaseHeader.h"
@interface DENFindDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic)UITableView *summarizeTableView;
@property (strong, nonatomic)NSMutableArray *titleArr;//section标题的数组
@property (strong, nonatomic)NSMutableArray *salesArr;//请求下来的salesArr
@property (strong, nonatomic)NSString *HeaderImageUrl;//headerImage图片链接
@property (strong, nonatomic)NSString *price;//价格;
@property (strong, nonatomic)NSString *sgAverageScore;//评分
@end

@implementation DENFindDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.name;
    
    [self initializeAll];
    [self creatTableView];
    [self requestDataForID:self.did];
}
#pragma mark- 初始化
- (void)initializeAll
{
    self.titleArr = [NSMutableArray array];
    self.salesArr = [NSMutableArray array];
    
}
#pragma mark- 创建tableView
- (void)creatTableView
{
    self.summarizeTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.summarizeTableView.delegate = self;
    self.summarizeTableView.dataSource = self;
    [self.view addSubview:self.summarizeTableView];
    
    
}
#pragma mark- 创建tableViewHeader
- (void)creatTableViewHeader
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(320))];
    headerView.backgroundColor = [UIColor whiteColor];
    UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT(250))];
    [headerView addSubview:headerImage];
    self.summarizeTableView.tableHeaderView = headerView;
    
//    请求图片
    NSURL *url = [NSURL URLWithString:self.HeaderImageUrl];
    [headerImage sd_setImageWithURL:url placeholderImage:nil];
//    创建官网报价
    UILabel *pri = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH(15), HEIGHT(260), WIDTH(75), HEIGHT(15))];
    pri.text = @"官方价 :";
    [headerView addSubview:pri];
    
    UILabel *priceRange = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH(95), HEIGHT(260), WIDTH(200), HEIGHT(15))];
    priceRange.textColor = [UIColor redColor];
    priceRange.text = [NSString stringWithFormat:@"¥ %@", self.price];
    [headerView addSubview:priceRange];
    
//    评分
    UILabel *sgAverageScoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH(15), HEIGHT(290), WIDTH(150), HEIGHT(15))];
    sgAverageScoreLabel.text = [NSString stringWithFormat:@"综合评分 %@ 分", self.sgAverageScore];
    [headerView addSubview:sgAverageScoreLabel];
}
#pragma mark- 请求数据
- (void)requestDataForID:(NSString *)ID
{
    [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/xsp/s/auto/info/v4.7/serials.xsp?serialId=%@&regionId=248&hasDealers=1&fmt=json", ID] block:^(id back) {
        self.HeaderImageUrl = [back objectForKey:@"photo_400x300"];
        self.price = [back objectForKey:@"priceRange"];
        self.sgAverageScore = [back objectForKey:@"sgAverageScore"];
        [self creatTableViewHeader];

        self.salesArr = [back objectForKey:@"sales"];
        for (NSDictionary *dic in self.salesArr) {
            NSString *title = [dic objectForKey:@"title"];
            [self.titleArr addObject:title];
        }
        [self.summarizeTableView reloadData];
    }];
}
#pragma mark- 设置每个section中cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *salesDic = [self.salesArr objectAtIndex:section];
    NSMutableArray *dataArr = [salesDic objectForKey:@"data"];
    return dataArr.count;

}
#pragma mark- 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cell";
    DENSummarizeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[DENSummarizeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    DENFindModel *findModel = [[DENFindModel alloc]init];
    NSDictionary *salesDic = [self.salesArr objectAtIndex:indexPath.section];
    NSMutableArray *dataArr = [salesDic objectForKey:@"data"];
    NSDictionary *dic = [dataArr objectAtIndex:indexPath.row];
    [findModel setValuesForKeysWithDictionary:dic];
    cell.findModel = findModel;
    return cell;
}
#pragma mark- cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DENFindInformationViewController *findInformationVC = [[DENFindInformationViewController alloc]init];
    [self.navigationController pushViewController:findInformationVC animated:YES];
    NSDictionary *salesDic = [self.salesArr objectAtIndex:indexPath.section];
    NSMutableArray *dataArr = [salesDic objectForKey:@"data"];
    NSDictionary *dic = [dataArr objectAtIndex:indexPath.row];
    
    findInformationVC.did = [dic objectForKey:@"id"];
}
#pragma mark- 设置section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArr.count;
}
#pragma mark- section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.titleArr objectAtIndex:section];
}
#pragma mark- 设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT(100);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
